import * as BABYLON from "@babylonjs/core";
import * as GUI from "@babylonjs/gui";
import { BabylonCanvas } from "./BabylonCanvas";
import { TWcsScene, TWcsSceneDevice3d, TWcsSceneDevice2d } from "../models";
import { AbstractDevice } from "./AbstractDevice";
import { BabylonScene } from "./BabylonScene";
import { BabylonScene2d } from "./2d/BabylonScene2d";
import { Device2D } from "./2d/controls/Device2D";

export class SceneManager {
  private canvas: BabylonCanvas;
  private scenes: Map<string, BabylonScene> = new Map<string, BabylonScene>();
  private activedSceneId: string = "";
  private mode: "2d" | "3d" = "2d";
  private tWcsScenes: TWcsScene[];
  constructor(canvas: BabylonCanvas, tWcsScenes: TWcsScene[]) {
    this.canvas = canvas;
    this.tWcsScenes = tWcsScenes;
  }

  public async init() {
    for (var tWcsScene of this.tWcsScenes) {
      if (!tWcsScene.sceneId) {
        console.error(`Init scene failed, scene id is empty`);
        continue;
      }

      if (this.mode == "2d") {
        var scene = new BabylonScene2d(this.canvas, tWcsScene);
        await scene.init();
        this.scenes.set(tWcsScene.sceneId, scene);
      } else {
      }
    }
  }

  public setActivedScene(id: string) {
    if (this.scenes.has(id)) {
      this.activedSceneId = id;
    } else {
      console.error(`Set actived scene id failed, wrong id: ${id}`);
    }
  }

  public getActivedScene() {
    for (var key of this.scenes.keys()) {
      if (this.scenes.get(key)?.active) {
        return this.scenes.get(key);
      }
    }
    return null;
  }
}
